package httpclient

import "net/http"

func newHTTPError(err error) *HttpError[any] {
	return &HttpError[any]{
		StatusCode: http.StatusInternalServerError,
		Err:        err,
	}
}

func newHTTPErrorWithStatusCode[T any](statusCode int, err error, body T) *HttpError[T] {
	return &HttpError[T]{
		StatusCode: statusCode,
		Err:        err,
		Body:       body,
	}
}

type HttpError[T any] struct {
	StatusCode int   `json:"statusCode"`
	Err        error `json:"error"`
	Body       T     `json:"body"`
}

func (e *HttpError[T]) Error() string {
	return e.Err.Error()
}

// IsHTTPError 判断是否是 HTTP 错误
func IsHTTPError[T any](err error) (*HttpError[T], bool) {
	httpErr, ok := err.(*HttpError[T])
	return httpErr, ok
}
